(The sample code is compiled with D7.
Delphi is not free software. If you want to use a free C compiler to write your code.
Please check the ..\Dev-C\ECC Plugin in DevCpp.txt)

The NAND Image Builder (NANDImg.exe) uses the Plug-in DLL to calculate ECC data.
Your ECC Plug-in DLL should export two functions with standard call.

   function Initial: integer; stdcall;
   function ECC(PageSize: integer; pData: PByte; pECC: PByte; PageIndex: integer; PartitionIndex: integer): integer; stdcall;

(If you use tool "View Dependencies" to check exported function name of your DLL.
The exported function name should be exactly the same "Initial", "ECC" without C++ name modifier.)

===================================================================
function Initial: integer; stdcall;
Input Parameters: none
Return Value: none
Description:
  If you want to create lookup table. You can do it in function Initial.

===================================================================
function ECC(PageSize: integer; pData: PByte; pECC: PByte; PageIndex: integer; PartitionIndex: integer): integer; stdcall;
Input Parameters:
   PageSize: The byte count of the pData^ will be 528, 2112, 2176, 4224, 4314 or 4320.
                 If your application are different. Please tell us to enhance it.

   pData: point to the data buffer for calculating the ECC.

   pECC: NAND Image Builder prepare second buffer (512 byte) for you to put your ECC result.
               The NAND Image Builder will use the offset list defined in "ECC Layout" to copy the ECC result back to the pData^.

   PageIndex: If Device Block Size is 64 pages and the partition is 10 blocks. The range of PageIndex will be 0~639.

   PartitionIndex: it will start from 0.

Return Value:
   It return the byte count of ECC result for different PageSize.
   If return 0, it means the ECC function fail to handle the PageSize.
   For example, If your code can handle 4320 byte of Page Size, only. You should return 0 for page size 528, 2112, 2176, 4224 and 4314.

Description:
   There are two stages for NAND Image Builder to call for ECC.
   1. Building the ECC Plugin List for "Partitions Tab" of NANDImg UI.
      The pData^ will be filled with 0xFF, the data size is slected Device Page Size. 
      If the ECC Plugin return none-zero value for the slected Device Page Size.
      This ECC DLL file will add into the ECC Method Drop Down List.

   2. NAND Image Generation:
      Each partition can select different ECC Plugin.
      If the ECC code is already inside the data. Please select "Embedded".

      Before call the ECC(), it will fill the the pData^ with value 0xFF, the data size is the same size as Device Page Size.
      Then it load the data from user's data file. (So the spare area will be embedded ECC from data file or 0xFF.)
      Then call the ECC().


===================================================================
Q&A
1. How the NANDImg.EXE get these DLL into ECC Plugin List?
   NAND Image Builder will find all the DLL in it's install folder which provide the correct function name.

   Then it uses current Device Page Size to test it.
   The pData^ will be filled with 0xFF value.

   The PageSize will be the selected 'Device Page Size' of NAND Image Builder.
   So, Page Size will be 528, 2112, 2176, 4224, 4314 or 4320.

   If your ECC Plugin can only handle page size 4320.
   It should return 0 for page size 528, 2112, 2176, 4224 and 4314.
   Retrun correct ECC Length (Byte) for 4320.

   If the ECC return value is greater than 0. (The Length of ECC code.)
   That means it can handle the Page Size of pData^.
   The UI will add the DLL filename into the 'ECC Method' drop-down list.

   Please note, the default selection of 'ECC Method' in UI is 'Embedded'.
   'Embedded' means the data file includes the spare are and ECC data.

   Note. The offset list of "ECC Layout". The length should be the same as the return value of DLL's ECC function call.

2. Anothor way to put your ECC back and manage your spare area data is do the work directly in pData^.

   For example, you want to put some Flag for the first and second pages of each Block.

   Use the "PageIndex % Device Block Size" to find the first and second pages. 
   Please note, Since there is no Device Block Size (pages) inside input parameters of ECC(..).
   It will be a magic number in your source code.

   Since the pData^ include the spare area data and it will fill with FF before call the ECC function of your DLL.
   You may have chance to modify the spare area data before write to imgae file.

   If you want to modify the data form by yourself with directly modify pData^.
   The "ECC Layout" of NAND Image Builder must be empty.

